<template>
  <div class="patient-detail-container">
    <!-- 患者基本信息 -->
    <el-card class="patient-info-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="card-title">患者基本信息</span>
          <div class="header-actions">
            <el-button type="primary" size="small" @click="handleEdit">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button type="success" size="small" @click="handleTransfer">
              <el-icon><Share /></el-icon>
              转诊
            </el-button>
          </div>
        </div>
      </template>
      
      <div class="patient-info">
        <el-row :gutter="24">
          <el-col :span="6">
            <div class="info-item">
              <label>患者ID：</label>
              <span>{{ patientInfo.patientId }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>姓名：</label>
              <span>{{ patientInfo.name }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>性别：</label>
              <el-tag :type="patientInfo.gender === '男' ? 'primary' : 'danger'" size="small">
                {{ patientInfo.gender }}
              </el-tag>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>年龄：</label>
              <span>{{ patientInfo.age }}岁</span>
            </div>
          </el-col>
        </el-row>
        
        <el-row :gutter="24">
          <el-col :span="6">
            <div class="info-item">
              <label>联系电话：</label>
              <span>{{ patientInfo.phone }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>身份证号：</label>
              <span>{{ patientInfo.idCard }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>登记时间：</label>
              <span>{{ patientInfo.createTime }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="info-item">
              <label>状态：</label>
              <el-tag :type="getStatusColor(patientInfo.status)">
                {{ getStatusName(patientInfo.status) }}
              </el-tag>
            </div>
          </el-col>
        </el-row>
        
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="info-item">
              <label>家庭住址：</label>
              <span>{{ patientInfo.address }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label>紧急联系人：</label>
              <span>{{ patientInfo.emergencyContact }}</span>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 疾病信息 -->
    <el-card class="disease-info-card" shadow="never">
      <template #header>
        <span class="card-title">疾病信息</span>
      </template>
      
      <div class="disease-content">
        <el-row :gutter="24">
          <el-col :span="8">
            <div class="disease-item">
              <h4>主要疾病</h4>
              <el-tag 
                v-for="disease in patientInfo.diseases" 
                :key="disease.id"
                :type="getDiseaseTypeColor(disease.type)"
                size="large"
                class="disease-tag"
              >
                {{ getDiseaseTypeName(disease.type) }}
              </el-tag>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="disease-item">
              <h4>确诊时间</h4>
              <p>{{ patientInfo.diagnosisDate }}</p>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="disease-item">
              <h4>病情严重程度</h4>
              <el-tag :type="getSeverityColor(patientInfo.severity)">
                {{ getSeverityName(patientInfo.severity) }}
              </el-tag>
            </div>
          </el-col>
        </el-row>
        
        <div class="disease-description">
          <h4>病情描述</h4>
          <p>{{ patientInfo.description }}</p>
        </div>
      </div>
    </el-card>

    <!-- 随访记录 -->
    <el-card class="followup-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="card-title">随访记录</span>
          <el-button type="primary" size="small" @click="handleAddFollowup">
            <el-icon><Plus /></el-icon>
            新增随访
          </el-button>
        </div>
      </template>
      
      <el-timeline>
        <el-timeline-item
          v-for="item in followupRecords"
          :key="item.id"
          :timestamp="item.date"
          placement="top"
        >
          <el-card shadow="hover" class="followup-item">
            <div class="followup-header">
              <div class="followup-title">
                <h4>{{ item.title }}</h4>
                <el-tag :type="item.type === 'routine' ? 'success' : 'warning'" size="small">
                  {{ item.type === 'routine' ? '定期随访' : '特殊随访' }}
                </el-tag>
              </div>
              <div class="followup-doctor">
                随访医生: {{ item.doctor }}
              </div>
            </div>
            <div class="followup-content">
              <p><strong>症状描述:</strong> {{ item.symptoms }}</p>
              <p><strong>检查结果:</strong> {{ item.examination }}</p>
              <p><strong>处理意见:</strong> {{ item.treatment }}</p>
              <p><strong>下次随访:</strong> {{ item.nextDate }}</p>
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-card>

    <!-- 检查报告 -->
    <el-card class="reports-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="card-title">检查报告</span>
          <el-button type="primary" size="small" @click="handleUploadReport">
            <el-icon><Upload /></el-icon>
            上传报告
          </el-button>
        </div>
      </template>
      
      <el-table :data="reports" stripe>
        <el-table-column prop="date" label="检查日期" width="120" />
        <el-table-column prop="type" label="检查类型" width="150">
          <template #default="{ row }">
            <el-tag>{{ row.type }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="result" label="检查结果" />
        <el-table-column prop="doctor" label="检查医生" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'normal' ? 'success' : 'warning'">
              {{ row.status === 'normal' ? '正常' : '异常' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleViewReport(row)">
              查看
            </el-button>
            <el-button type="danger" size="small" @click="handleDeleteReport(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 用药记录 -->
    <el-card class="medication-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="card-title">用药记录</span>
          <el-button type="primary" size="small" @click="handleAddMedication">
            <el-icon><Plus /></el-icon>
            新增用药
          </el-button>
        </div>
      </template>
      
      <el-table :data="medications" stripe>
        <el-table-column prop="name" label="药品名称" />
        <el-table-column prop="dosage" label="剂量" width="120" />
        <el-table-column prop="frequency" label="用药频次" width="120" />
        <el-table-column prop="startDate" label="开始时间" width="120" />
        <el-table-column prop="endDate" label="结束时间" width="120" />
        <el-table-column prop="doctor" label="开药医生" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'info'">
              {{ row.status === 'active' ? '使用中' : '已停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEditMedication(row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Edit, Share, Plus, Upload } from '@element-plus/icons-vue'

const route = useRoute()
const patientId = route.params.id as string

// 患者信息
const patientInfo = ref({
  patientId: 'P001234',
  name: '张三',
  gender: '男',
  age: 65,
  phone: '13812345678',
  idCard: '320123199001010001',
  createTime: '2023-12-01',
  status: 'active',
  address: '江苏省南京市玄武区中山东路123号',
  emergencyContact: '李四 13987654321',
  diseases: [
    { id: 1, type: 'diabetes', name: '2型糖尿病' },
    { id: 2, type: 'hypertension', name: '高血压' }
  ],
  diagnosisDate: '2023-06-15',
  severity: 'moderate',
  description: '患者确诊2型糖尿病合并高血压3年，目前血糖控制尚可，血压偏高，需要调整用药方案。'
})

// 随访记录
const followupRecords = ref([
  {
    id: 1,
    date: '2024-01-10',
    title: '定期随访检查',
    type: 'routine',
    doctor: '王医生',
    symptoms: '无明显不适，血糖控制良好',
    examination: '血糖：6.8mmol/L，血压：145/85mmHg',
    treatment: '继续现有治疗方案，增加运动量',
    nextDate: '2024-02-10'
  },
  {
    id: 2,
    date: '2023-12-15',
    title: '血压异常随访',
    type: 'special',
    doctor: '李医生',
    symptoms: '头晕，乏力',
    examination: '血压：160/95mmHg，心电图正常',
    treatment: '调整降压药物剂量',
    nextDate: '2024-01-15'
  }
])

// 检查报告
const reports = ref([
  {
    id: 1,
    date: '2024-01-10',
    type: '血常规',
    result: '血糖：6.8mmol/L，血脂正常',
    doctor: '检验科',
    status: 'normal'
  },
  {
    id: 2,
    date: '2024-01-10',
    type: '心电图',
    result: '窦性心律，ST段轻微改变',
    doctor: '心电图室',
    status: 'abnormal'
  }
])

// 用药记录
const medications = ref([
  {
    id: 1,
    name: '二甲双胍片',
    dosage: '500mg',
    frequency: '一日三次',
    startDate: '2023-06-15',
    endDate: '',
    doctor: '内分泌科',
    status: 'active'
  },
  {
    id: 2,
    name: '氨氯地平片',
    dosage: '5mg',
    frequency: '一日一次',
    startDate: '2023-12-15',
    endDate: '',
    doctor: '心内科',
    status: 'active'
  }
])

// 工具函数
const getDiseaseTypeName = (type: string) => {
  const map: Record<string, string> = {
    diabetes: '糖尿病',
    hypertension: '高血压',
    coronary: '冠心病',
    copd: '慢阻肺'
  }
  return map[type] || type
}

const getDiseaseTypeColor = (type: string) => {
  const map: Record<string, string> = {
    diabetes: 'primary',
    hypertension: 'success',
    coronary: 'warning',
    copd: 'danger'
  }
  return map[type] || 'info'
}

const getStatusName = (status: string) => {
  const map: Record<string, string> = {
    active: '活跃',
    stable: '稳定',
    attention: '需关注'
  }
  return map[status] || status
}

const getStatusColor = (status: string) => {
  const map: Record<string, string> = {
    active: 'success',
    stable: 'primary',
    attention: 'warning'
  }
  return map[status] || 'info'
}

const getSeverityName = (severity: string) => {
  const map: Record<string, string> = {
    mild: '轻度',
    moderate: '中度',
    severe: '重度'
  }
  return map[severity] || severity
}

const getSeverityColor = (severity: string) => {
  const map: Record<string, string> = {
    mild: 'success',
    moderate: 'warning',
    severe: 'danger'
  }
  return map[severity] || 'info'
}

// 事件处理函数
const handleEdit = () => {
  ElMessage.info('编辑患者信息')
}

const handleTransfer = () => {
  ElMessage.info('转诊功能')
}

const handleAddFollowup = () => {
  ElMessage.info('新增随访记录')
}

const handleUploadReport = () => {
  ElMessage.info('上传检查报告')
}

const handleViewReport = (row: any) => {
  ElMessage.info(`查看报告: ${row.type}`)
}

const handleDeleteReport = (row: any) => {
  ElMessage.info(`删除报告: ${row.type}`)
}

const handleAddMedication = () => {
  ElMessage.info('新增用药记录')
}

const handleEditMedication = (row: any) => {
  ElMessage.info(`编辑用药: ${row.name}`)
}

onMounted(() => {
  console.log('患者ID:', patientId)
})
</script>

<style lang="scss" scoped>
.patient-detail-container {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .card-title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
    
    .header-actions {
      display: flex;
      gap: 8px;
    }
  }
  
  .patient-info-card {
    margin-bottom: 20px;
    
    .patient-info {
      .info-item {
        margin-bottom: 16px;
        
        label {
          font-weight: 600;
          color: #606266;
          margin-right: 8px;
        }
        
        span {
          color: #303133;
        }
      }
    }
  }
  
  .disease-info-card {
    margin-bottom: 20px;
    
    .disease-content {
      .disease-item {
        h4 {
          margin: 0 0 12px 0;
          color: #303133;
          font-size: 14px;
          font-weight: 600;
        }
        
        .disease-tag {
          margin-right: 8px;
          margin-bottom: 8px;
        }
        
        p {
          margin: 0;
          color: #606266;
        }
      }
      
      .disease-description {
        margin-top: 24px;
        
        h4 {
          margin: 0 0 12px 0;
          color: #303133;
          font-size: 14px;
          font-weight: 600;
        }
        
        p {
          margin: 0;
          color: #606266;
          line-height: 1.6;
        }
      }
    }
  }
  
  .followup-card {
    margin-bottom: 20px;
    
    .followup-item {
      .followup-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 12px;
        
        .followup-title {
          display: flex;
          align-items: center;
          gap: 12px;
          
          h4 {
            margin: 0;
            color: #303133;
            font-size: 16px;
          }
        }
        
        .followup-doctor {
          font-size: 14px;
          color: #909399;
        }
      }
      
      .followup-content {
        p {
          margin: 8px 0;
          color: #606266;
          line-height: 1.5;
          
          strong {
            color: #303133;
          }
        }
      }
    }
  }
  
  .reports-card, .medication-card {
    margin-bottom: 20px;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .patient-detail-container {
    .card-header {
      flex-direction: column;
      gap: 12px;
      align-items: stretch;
    }
    
    .patient-info {
      .el-col {
        margin-bottom: 12px;
      }
    }
  }
}
</style>